package com.pay.ioopos.fragment;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.zxing.BarcodeFormat;
import com.pay.ioopos.BindState;
import com.pay.ioopos.R;
import com.pay.ioopos.SubScreenBackgroudLoader;
import com.pay.ioopos.SubScreenFactory;

public class PayScanFragment extends AbstractScanFragment implements BindState {
    private String amount;
    public PayScanFragment(String amount) {
        super(BarcodeFormat.QR_CODE);
        this.amount = amount;
    }

    @Override
    public boolean useNetwork() {
        return true;
    }

    @Override
    protected void execute() throws Exception {
        speak("请支付" + amount + "元");
        TipHorizontalFragment tip = new TipHorizontalFragment(TipFragment.TipType.WAIT, R.string.pay_wait);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(getView().getId(), tip);
        fragmentTransaction.commit();
        SubScreenFactory.startSubScreen(this::flushSubScreenToShowMoney);
    }

    private void flushSubScreenToShowMoney() {

        Canvas canvas;
        Bitmap bitmap = SubScreenBackgroudLoader.getInstance().getPayInitBitmap();
        if (null == bitmap) {
            bitmap = SubScreenFactory.createBitmap();
            canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);
        } else {
            bitmap = SubScreenFactory.scaledBitmap(bitmap);
            canvas = new Canvas(bitmap);
        }

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(25f);

        String text = "支付金额(元)";
        int fontWidth = (int) paint.measureText(text);
        int startX = (bitmap.getWidth() - fontWidth) / 2;
        canvas.drawText(text, startX, 130, paint);

        paint.setTextSize(80f);
        text = amount;
        fontWidth = (int) paint.measureText(text);
        startX = (bitmap.getWidth() - fontWidth) / 2;
        canvas.drawText(text, startX, 220, paint);

        canvas.save();
        canvas.restore();

        SubScreenFactory.flush(bitmap);

    }

}
